﻿#include "imageResource.h"
#include<QDir>
imageResource::imageResource(const QString& name, const QString& path)
{
	addPixmap(name, path);
}
void imageResource::addPixmap(const QString& name, const QString& path)
{
	m_map[name].append(QPixmap(getDir(path)));
}

void imageResource::addMovie(const QString& name, const QString& path)
{
	m_Movie[name]= new QMovie(getDir(path));
}

QPixmap& imageResource::getPixmap(const QString& name, const int index)
{
	return m_map[name][index];
}

QMovie* imageResource::getMovie(const QString& name)
{
	return m_Movie[name];
}

void imageResource::setPath(const QString& path)
{
	m_Path = path;
}

QVector<QPixmap>* imageResource::getPixmaps(const QString& name)
{
	if (m_map.find(name) == m_map.end())
	{
		qWarning() << "没有找到:" << name;
		return nullptr;
	}
	return &m_map[name];
}

const QString imageResource::getDir(const QString& path)
{
	QString currPath = QDir::currentPath();
	if (!m_Path.isEmpty())
		currPath = m_Path;
	currPath = currPath + "/" + path;
	QDir dir(currPath);
	if (!dir.isReadable())
	{
		qWarning() << "当前路径：" << currPath << "错误,找不到文件";
		qWarning() << "请手动设置setPath或将资源文件放到.exe程序所在目录";
		throw "找不到资源";
	}
	return currPath;
}
